//递增的三元子序列
class Solution {
public:
    bool increasingTriplet(vector<int>& nums) {
        //贪心解
        vector<int> arr;
        arr.push_back(nums[0]);
        for(size_t i = 1 ; i < nums.size() ; ++i)
        {
            bool is = false;
            for(auto& e : arr)
            {
                if(e > nums[i])
                {
                    e = nums[i];
                    is = true;
                    break;
                }
                else if(e == nums[i])
                {
                    is = true;
                    break;
                }
            }
            if(is == false) arr.push_back(nums[i]);
            if(arr.size() == 3) return true;
        }
        return false;
    }
};

//最长连续递增子序列
class Solution {
public:
    int findLengthOfLCIS(vector<int>& nums) {
        int left = 0 , right = 1 , n = nums.size(),ret = -1;
        while(right < n)
        {
            if(nums[right] > nums[right-1]) ++right;
            else
            {
                ret = max(ret , right - left );
                left = right;
                ++right;
            }
        }
        ret = max(ret,right - left);
        return ret;
    }
};

//买卖股票的最佳时机
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int prevMin = prices[0] , n = prices.size();
        int ret = 0;
        for(size_t i = 1 ; i < n ; ++i)
        {
            ret = max(ret,prices[i]-prevMin);
            prevMin = min(prevMin,prices[i]);
        }
        return ret;
    }
};

//买卖股票的最佳时机Ⅱ
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        if(n == 1) return 0;
        int buy = -1 , sell = -1,ret = 0;
        for(size_t i = 0 ; i < n ; ++i)
        {
            if(i != n-1 && prices[i] == prices[i+1]) continue;
            if(buy == -1 && (i == 0 || prices[i-1] >= prices[i]) && (i != n-1 && prices[i+1] > prices[i]))
            {
                buy = prices[i];
            }
            else if(buy != -1 && (i == 0 || prices[i-1] <= prices[i]) && (i == n-1 || prices[i+1] < prices[i]))
            {
                ret += prices[i] - buy;
                buy = -1;
            }
        }
        return ret;
    }
};